package jeons.member.action;

import java.util.List;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import jeons.common.util.SimpleMailService;
import jeons.member.biz.MemberBiz;
import jeons.member.dto.MemberDTO;

import org.springbyexample.web.servlet.view.tiles2.TilesUrlBasedViewResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/member")
public class MemberAction {
	private SimpleMailService simpleMail;
	@Autowired TilesUrlBasedViewResolver tilesUrlBasedViewResolver;
	@Autowired MemberBiz memberBiz;
	
	

	public SimpleMailService getSimpleMail() {
		return simpleMail;
	}

	public void setSimpleMail(SimpleMailService simpleMail) {
		this.simpleMail = simpleMail;
	}

	/**
	* @Method Name  : agreement
	* @작성일   : 2012. 2. 23.
	* @작성자   : user
	* @변경이력  :
	* @Method 설명 :
	* @return
	*/
	@RequestMapping(value="/agreement", method=RequestMethod.GET)
	public String agreement(){
		tilesUrlBasedViewResolver.setTilesDefinitionName("default");
		System.out.println("aaaa");
		return "member/agreement";
	}
	
	/**
	* @Method Name  : agreement_oK
	* @작성일   : 2012. 2. 23.
	* @작성자   : user
	* @변경이력  :
	* @Method 설명 :
	* @param check
	* @param model
	* @return
	*/
	@RequestMapping(value="/agreement", method=RequestMethod.POST)
	public String agreement_oK(@RequestParam("check")boolean check,Model model){
		tilesUrlBasedViewResolver.setTilesDefinitionName("default");
		if (check) {
			model.addAttribute("member",new MemberDTO());
			return "redirect:join";
			
		}else{
			/*return "member/agreement";*/
			model.addAttribute("member",new MemberDTO());
			return "redirect:/";
		}
	}
	
	
	
	/**
	* @Method Name  : join_Test
	* @작성일   : 2012. 2. 23.
	* @작성자   : user
	* @변경이력  :
	* @Method 설명 :
	* @param model
	* @param userno
	* @return
	*/
	@RequestMapping(value="/join", method=RequestMethod.GET)
	public String join_Test(Model model,@RequestParam(value="userno", required=false,  defaultValue="0")int userno){
		tilesUrlBasedViewResolver.setTilesDefinitionName("default");
		if (userno != 0) {
			model.addAttribute("member", memberBiz.getDataUserNo(userno));
		} else {
			model.addAttribute("member",new MemberDTO());
		}
		return "member/join";
	}
	
	
	/**
	* @Method Name  : join
	* @작성일   : 2012. 2. 23.
	* @작성자   : user
	* @변경이력  :
	* @Method 설명 :
	* @param memberDTO
	* @return
	*/
	@RequestMapping(value="/join", method=RequestMethod.POST)
	public  String join(@ModelAttribute("member")MemberDTO memberDTO){
		tilesUrlBasedViewResolver.setTilesDefinitionName("default");
		memberBiz.insertMember(memberDTO);
		return "redirect:/";
	}
	
	
	
	/**
	* @Method Name  : user_Login
	* @작성일   : 2012. 2. 23.
	* @작성자   : user
	* @변경이력  :
	* @Method 설명 :
	* @param model
	* @return
	*/
	@RequestMapping(value="/login", method=RequestMethod.GET)
	public String user_Login(Model model){
		tilesUrlBasedViewResolver.setTilesDefinitionName("default");
		model.addAttribute("member", new MemberDTO());
		return "member/login";
	}
	
	
	/**
	* @Method Name  : user_Login_Test
	* @작성일   : 2012. 2. 23.
	* @작성자   : user
	* @변경이력  :
	* @Method 설명 :
	* @param model
	* @param memberDTO
	* @param request
	* @return
	*/
	@RequestMapping(value="/login", method=RequestMethod.POST)
	public @ResponseBody String user_Login_Test(Model model,@ModelAttribute("member")MemberDTO memberDTO,HttpServletRequest request){
		tilesUrlBasedViewResolver.setTilesDefinitionName("default");
		String check ="true";
		String id = memberBiz.getDataID(memberDTO.getU_id());
		if (id != null) {
			
			try {
				MemberDTO member = memberBiz.getDataPassword(memberDTO);

				HttpSession session = request.getSession(true);
				session.setAttribute("u_id", id);
				session.setAttribute("userno", member.getUserno());
				session.setAttribute("point", member.getPoint());
				
			} catch (Exception e) {
				check = "nopasswd";
			}
		
		}else{
			check = "noid";
		}
		
		
		return check;
	}
	
	
	/**
	* @Method Name  : user_LogOut
	* @작성일   : 2012. 2. 23.
	* @작성자   : user
	* @변경이력  :
	* @Method 설명 :
	* @param request
	* @return
	*/
	@RequestMapping(value="/logout", method=RequestMethod.GET)
	public @ResponseBody String user_LogOut(HttpServletRequest request){
		System.out.println("�α׾ƿ� ������ �Դϴ�.");
		String check = "true";
			try {
				HttpSession session = request.getSession(true);
				session.invalidate();
				check = "true";
			} catch (Exception e) {
				check = "false";
			}
			return check;
	}
	

	/**
	* @Method Name  : check_ID
	* @작성일   : 2012. 2. 23.
	* @작성자   : user
	* @변경이력  :
	* @Method 설명 :
	* @param u_id
	* @return
	*/
	@RequestMapping(value="/checkid", method=RequestMethod.GET)
	public @ResponseBody String check_ID(@RequestParam("u_id")String u_id){
		String check = "true";
		String id = memberBiz.getDataID(u_id);
		if (id != null) {
			check = "false";
		}
		return check;
	}
	
	/**
	* @Method Name  : check_ID
	* @작성일   : 2012. 2. 23.
	* @작성자   : user
	* @변경이력  :
	* @Method 설명 : 
	* @param model
	* @return
	*/
	@RequestMapping(value="/search_id", method=RequestMethod.GET)
	public String check_ID(Model model){
		tilesUrlBasedViewResolver.setTilesDefinitionName("tmpl");
		model.addAttribute("SearchMember", new MemberDTO());
		return "member/search_id";
	}
	
	
	/**
	* @Method Name  : check_ID_Data
	* @작성일   : 2012. 2. 23.
	* @작성자   : user
	* @변경이력  :
	* @Method 설명 : 유저 아이디
	* @param model
	* @param memberDTO
	* @return
	*/
	@RequestMapping(value="/search_id_data", method=RequestMethod.POST)
	public String check_ID_Data(Model model,@ModelAttribute("#SearchMember")MemberDTO memberDTO){
		tilesUrlBasedViewResolver.setTilesDefinitionName("tmpl");
		model.addAttribute("search_result", memberBiz.getSearchID(memberDTO));
		return "member/search_id_list";
	}
	
	
	/**
	* @Method Name  : search_Pwd
	* @작성일   : 2012. 2. 23.
	* @작성자   : user
	* @변경이력  :
	* @Method 설명 : 
	* @param model
	* @return
	*/
	@RequestMapping(value="/search_pwd", method=RequestMethod.GET)
	public String search_Pwd(Model model){
		tilesUrlBasedViewResolver.setTilesDefinitionName("tmpl");
		model.addAttribute("SearchMember", new MemberDTO());
		return "member/search_pwd";
	}
	
	/**
	* @Method Name  : search_Pwd_Data
	* @작성일   : 2012. 2. 23.
	* @작성자   : user
	* @변경이력  :
	* @Method 설명 : 
	* @return
	*/
	@RequestMapping(value="/search_pwd_data", method=RequestMethod.GET)
	public String search_Pwd_Data(Model model,@ModelAttribute("SearchMember")MemberDTO memberDTO){
		model.addAttribute("search_result", memberBiz.getSearchPWD(memberDTO));
		model.addAttribute("memberSendEmail", new MemberDTO());
		return "member/search_pwd_list";
	}
	
	/**
	* @Method Name  : updataUserPasswd
	* @작성일   : 2012. 2. 23.
	* @작성자   : user
	* @변경이력  :
	* @Method 설명 : 유저 비밀번호 업데이트
	* @param memberDTO
	*/
	@RequestMapping(value="/search_pwd_data", method=RequestMethod.POST)
	public String updataUserPasswd(@ModelAttribute("memberSendEmail")MemberDTO memberDTO){
		System.out.println("aaaaaaaaa");
		System.out.println(memberDTO.getTempemail());
		simpleMail.sendMail(memberDTO.getUsername(), memberDTO.getTemppaswword(), memberDTO.getTempemail());
		memberBiz.updatePWD(memberDTO);
		return "redirect:/member/login";
	}
	
	/**
	* @Method Name  : test
	* @작성일   : 2012. 2. 23.
	* @작성자   : user
	* @변경이력  :
	* @Method 설명 :
	* @param memberDTO
	* @return
	*/
	@RequestMapping(value="/Sendemail", method=RequestMethod.POST)
	public @ResponseBody String test(@ModelAttribute("SearchMember")MemberDTO memberDTO){
		System.out.println(memberDTO.getUseremail());
		return "member/search_pwd_list";
	}
	
	
	/**
	* @Method Name  : userUpdatepWD
	* @작성일   : 2012. 2. 23.
	* @작성자   : user
	* @변경이력  :
	* @Method 설명 :
	* @param tmp
	* @return
	*/
	@RequestMapping(value="/userUpdatepWD", method=RequestMethod.POST)
	public @ResponseBody String userUpdatepWD(@RequestParam("tmp")String tmp){
		String check="true";
		System.out.println("**************************");
		System.out.println(tmp);
		return check;
	}
	
}
